import axios from 'axios'
import apiUrls from 'common/api-urls'

export function uploadFile(data, onProgress) {
  const formData = new FormData()
  // 兼容两种情况：data.file 可能是文件对象，也可能是 { raw: 文件对象 }
  const file = data.file?.raw || data.file
  formData.append('files', file)
  if (data.busiType) {
    formData.append('busiType', data.busiType)
  }

  const config = {
    onUploadProgress: function (progressEvent) {
      if (onProgress && typeof onProgress === 'function') {
        const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        onProgress(percentCompleted, progressEvent)
      }
    }
  }

  return axios.post(apiUrls.uploadFile, formData, config)
}
